using System;
using Common.Commands.NewsLetterCommands;
using Common.Views;
using Core.Domain.Model;
using NUnit.Framework;
using Sanelib.Common.Helpers;
using Sanelib.DataView;

namespace IntegrationTests.Processors
{
    public class NewsLetterProcessorTester : IntegrationTestBase
    {
        [Test]
        public void ShouldAddNewsLetter()
        {
            var command = new AddNewsLetter
            {
                Id = GuidComb.New(),
                Email = "nikunj.balar@sanelib.com",
            };

            ProcessCommand(command);
            var repo = GetInstance<IViewRepository<NewsLetterView>>();
            var newsLetterView = repo.GetById(command.Id);

            Assert.AreEqual(newsLetterView.Email, command.Email);
        }

        [Test]
        public void ShouldDeleteNewsLetter()
        {
            var newsLetter = new NewsLetter
            {
                Id = GuidComb.New(),
                Email = "nikunj.balar@sanelib.com",
                IsActive = true,
                InsertedDate = DateTime.Now
            };

            Persist(newsLetter);

            var command = new DeleteNewsLetter(newsLetter.Id);

            ProcessCommand(command);
            var repo = GetInstance<IViewRepository<NewsLetterView>>();
            var newsLetterView = repo.GetByKey(Property.Of<NewsLetterView>(x => x.Email), newsLetter.Email);

            Assert.IsNull(newsLetterView);
        }

        [Test]
        public void ShouldUpdateNewsLetter()
        {
            var newsLetter = new NewsLetter
            {
                Id = GuidComb.New(),
                Email = "nikunj.balar@sanelib.com",
                IsActive = true,
                InsertedDate = DateTime.Now
            };

            Persist(newsLetter);


            var command = new UpdateNewsLetter
            {
                Id = newsLetter.Id,
                Email = "nikunjbbalar@gmail.com",
                IsActive = false
            };

            ProcessCommand(command);

            var repo = GetInstance<IViewRepository<NewsLetterView>>();
            var newsLetterView = repo.GetById(command.Id);

            Assert.AreEqual(newsLetterView.Id, newsLetter.Id);
            Assert.AreNotEqual(newsLetterView.Email, newsLetter.Email);
            Assert.AreNotEqual(newsLetterView.IsActive, newsLetter.IsActive);
        }
    }
}